/**
 * @author 黄波
 * 创建日期：2011Oct 5, 2011
 * TODO// N/A
 */
package com.platform.njcc.action;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.platform.pojo.UserInfo;
import com.platform.user.bo.UserBo;

/**
 * 用戶登錄
 */
public class LoginAction extends NJCCSuport {

	private String loginName;	//用戶名
	private String loginPasswd;	//密碼
	private String refreshurl;	//登陸成功后跳轉URL
	
	public String getRefreshurl() {
		return refreshurl;
	}
	public void setRefreshurl(String refreshurl) {
		this.refreshurl = refreshurl;
	}
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public String getLoginPasswd() {
		return loginPasswd;
	}
	public void setLoginPasswd(String loginPasswd) {
		this.loginPasswd = loginPasswd;
	}
	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		if(loginName == null || loginName.equals("") || loginPasswd == null || loginPasswd.equals("")) {
			out.flush();
			out.write("<script>alert('用户名或密码不可为空！');history.back();</script>");
			out.close();
			return null;
		}
		UserBo bo = UserBo.getInstance();
		UserInfo user = bo.getUser1(loginName);
		if(user == null) {
			out.flush();
			out.write("<script>alert('不存在该用户！');history.back();</script>");
			out.close();
			return null;
		} else if(user != null && !user.getLoginPasswd().equals(getLoginPasswd())) {
			out.flush();
			out.write("<script>alert('密码错误！');history.back();</script>");
			out.close();
			return null;
		} else {
//			if(user.getIsAudit() == 0) {
//				out.flush();
//				out.write("<script>alert('该用户处于锁定状态，请联系管理员解锁后登录！');history.back();</script>");
//				out.close();
//				return null;
//			}
			HttpSession session = ServletActionContext.getRequest().getSession();
			session.setAttribute("user", user);
			//更新用户登录IP和时间
			user.setLastLoginIp(ServletActionContext.getRequest().getRemoteAddr());
			user.setLastLoginTime(new Date());
			UserBo.getInstance().updateUser(user);
			if(refreshurl == null || refreshurl.equals("")) {
				refreshurl = "../entry.jsp";
			} else {
				refreshurl = "../" + refreshurl;
			}
			out.flush();
			out.write("<script>window.location.href='" + refreshurl + "';</script>");
			out.close();
			return null;
		}
		
	}
	
	
}
